package com.zjitc.common.handler;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author: ${赵诚挚}
 * @Date: 2024/6/21 10:58
 */
@Configuration
public class WebMvcHandler implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        WebMvcConfigurer.super.addCorsMappings(registry);
        registry.addMapping("/**") // 访问根路径下的所有地址
                .allowCredentials(true) // 设置允许访问凭证
                .allowedHeaders("*") // 设置允许访问请求头
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 设置允许访问的请求方法
                .allowedOriginPatterns("*"); // 设置允许所有源地址访问
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        WebMvcConfigurer.super.addResourceHandlers(registry);
        // 添加图片地址资源访问
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:C:\\Users\\Administrator\\Desktop\\java-study\\java\\edu-parent\\images\\");
    }
}
